# -*- coding: utf-8 -*-
import logging
from pycube.core.sql.model import column
from pycube.core.sql.types import *
'''
Created on 23.09.2009

@author: varg
'''
def __init__( psql ):
    psql.register( 'permission', ( 
               column( 'id', int4, autoSequence=True, primary_key=True, notnull=True, autoIndex=True ),
               column( 'value', varchar( 100 ), notnull=True ),
               column( 'name', varchar( 100 ), notnull=True ),
               column( 'desctiption', text ),

        ) )
    """
    CREATE FUNCTION check_password(u TEXT, p TEXT ,s TEXT)
    RETURNS BOOLEAN AS $$
    DECLARE 
    passed BOOLEAN;
    ids int4;
    BEGIN
            SELECT  (pass = $2),id INTO passed,ids
            FROM    users
            WHERE   login = $1;
            if passed THEN
               update session set user_id=ids where name=$3;
            end if;
            RETURN passed;
    END;
    $$  LANGUAGE plpgsql
    """

class auth():
    def __init__( self, psql ):
        self.q = psql
    def check_auth( self, user, password, session ):
        res = self.q.select( "select check_password(%s,%s,%s)", ( user, password, session ) , True )
        self.q.commit()
        logging.debug( res )
        return res[0]

